package com.dongdongshop.job;

import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.admin.TbUser;
import com.dongdongshop.utils.MailUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Component
@DisallowConcurrentExecution//比如job执行10秒，任务是每隔5秒执行，加上这个注解，程序就会等10秒结束后再执行下一个任务。
public class MailJob implements Job {
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {

            JobKey key = context.getJobDetail().getKey();

//        JobDataMap dataMap = context.getMergedJobDataMap();
//        String name = dataMap.getString("name");
//        String desc = dataMap.getFloat("desc");
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime localDateTime = now.minusDays(15);
            localDateTime.getYear();
            LocalDateTime time = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(),0,0);
            LocalDateTime tomTime = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth()+1,0,0);
            Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
            Date tomdate = Date.from(tomTime.atZone(ZoneId.systemDefault()).toInstant());

            List<TbUser> users = tbUserMapper.listUserByCreateTime(date,tomdate);
        for (TbUser user : users) {
            System.err.println("Instance " + key + " of DumbJob says: " + user.getUsername() + ", and val is: " + user.getEmail());
          /*  mailUtil.sendSimpleMail(user.getEmail(),user.getUsername()+"欢迎成为本站用户","欢迎欢迎");*/
        }
    }
}

